﻿using System;
using System.Management;

namespace Utilities
{
    /// <summary>
    /// 获取信息类
    /// 关于当前的系统
    /// </summary>
    public static class Environment
    {
        #region 公共静态属性
        /// <summary>
        /// 运行应用程序的计算机的名称
        /// </summary>
        public static string MachineName
        {
            get { return System.Environment.MachineName; }
        }

        /// <summary>
        /// 获取运行应用程序的用户名称
        /// </summary>
        public static string UserName
        {
            get { return System.Environment.UserName; }
        }

        /// <summary>
        /// 应用程序正在运行的域的名称
        /// </summary>
        public static string DomainName
        {
            get { return System.Environment.UserDomainName; }
        }

        /// <summary>
        /// 运行的操作系统的名称
        /// </summary>
        public static string OSName
        {
            get { return System.Environment.OSVersion.Platform.ToString(); }
        }

        /// <summary>
        /// 操作系统运行的版本信息
        /// </summary>
        public static string OSVersion
        {
            get { return System.Environment.OSVersion.Version.ToString(); }
        }

        /// <summary>
        /// 操作系统上运行的服务包
        /// </summary>
        public static string OSServicePack
        {
            get { return System.Environment.OSVersion.ServicePack; }
        }

        /// <summary>
        /// 全名，包括 service pack、版本等。
        /// </summary>
        public static string OSFullName
        {
            get { return System.Environment.OSVersion.VersionString; }
        }

        /// <summary>
        /// 获取当前堆栈跟踪信息
        /// </summary>
        public static string StackTrace
        {
            get { return System.Environment.StackTrace; }
        }

        /// <summary>
        /// 返回计算机上的处理器数
        /// </summary>
        public static int NumberOfProcessors
        {
            get { return System.Environment.ProcessorCount; }
        }

        /// <summary>
        /// GC认为这是使用的内存总量
        /// 在应用中的字节
        /// </summary>
        public static long TotalMemoryUsed
        {
            get { return GC.GetTotalMemory(false); }
        }

        /// <summary>
        /// 可用字节的内存总量
        /// </summary>
        public static long TotalMemory
        {
            get
            {
                long ReturnValue = 0;
                ObjectQuery TempQuery = new ObjectQuery("SELECT * FROM Win32_LogicalMemoryConfiguration");
                using (ManagementObjectSearcher Searcher = new ManagementObjectSearcher(TempQuery))
                {
                    foreach (ManagementObject TempObject in Searcher.Get())
                    {
                        ReturnValue = long.Parse(TempObject["TotalPhysicalMemory"].ToString()) * 1024;
                    }
                }
                return ReturnValue;
            }
        }
        #endregion
    }
}
